<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <cpn ref="aaa"></cpn>
    </div>

    <template id="cpn">
        <div>
            <div>我是cpn子组件</div>
            <button @click="btnClick">按钮</button>
            <h2>-------</h2>
            <ccpn></ccpn>
        </div>
    </template>
    <template id="ccpn">
        <div>
            <div>我是ccpn子组件</div>
            <button @click="btnClick">按钮</button>
        </div>
    </template>

    <script src="../js/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                message: "你好呀",
            },
            components: {
                cpn: {
                    template: "#cpn",
                    methods: {
                        btnClick() {
                            // 1.访问父组件
                            console.log(this.$parent);
                            console.log(this.$parent.message);
                        }
                    },
                    data() {
                        return {
                            name: '我是cpn组件的name',
                        }
                    },
                    components: {
                        ccpn: {
                            template: '#ccpn',
                            methods: {
                                btnClick() {
                                    // 1.访问父组件
                                    console.log(this.$parent);
                                    console.log(this.$parent.name);

                                    // 2.访问根组件
                                    console.log(this.$root);
                                    console.log(this.$root.message);
                                }
                            }
                        }
                    }
                }
            },

        })
    </script>
</body>

</html>